var appPath = getAppPath();

$(function(){
	
	$("#codeTypeGrid").datagrid({
		url:appPath+'/system/code_pagedCodeTypeList.do',
		striped: true,
		remoteSort:false,
		singleSelect:true,
		idField:'codeTypeId',
		queryParams:{systemId:"order"},
		columns:[[
		          {field:'codeTypeNo',title:'操作',width:32,align:'center',
					  formatter:function(value,rowData,rowIndex){
			    		  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/images/system/icon/order-search.png' title='查看' onClick='showEdit("+rowData.codeTypeId+")'/></a>";
					  }
		          },
		          {field:'codeTypeId',title:'类型编码',width:160,align:'left',sortable:true},
		          {field:'codeTypeValue',title:'类型名称',width:160,align:'left',sortable:true},
		          {field:'codeTypeRemark',title:'备注',width:200,align:'left',sortable:true}
		        ]],
        pagination:true,
		rownumbers:true,
		onBeforeEdit:function(index,rowData){
			rowData.editing = true;
			$('#codeGrid').datagrid('refreshRow', index);
		},
		onCancelEdit:function(index,rowData){
			rowData.editing = false;
			$('#codeGrid').datagrid('refreshRow', index);
		},
		onAfterEdit:function(index,rowData){
			rowData.editing = false;
			$('#codeGrid').datagrid('refreshRow', index);
		},
		onSelect:function(index,rowData){
			showCode(rowData.codeTypeId);
		}
		
		
	}
	);
	
}
);

function keyPressQuery(et){
	if (et.keyCode) {
		if (et.keyCode == 13)
			doQuery();
	} else {
		if (et.which == 13)
			doQuery();
    }  
}

//查询
function doQuery(){
	var param = $('#codeTypeGrid').datagrid('options').queryParams;
	param.codeTypeId = $.trim($('#queryCodeTypeId').val());
	param.codeTypeValue = $.trim($('#queryCodeTypeValue').val());

	$("#codeTypeGrid").datagrid('unselectAll');
	$("#codeTypeGrid").datagrid('load',param);
	$("#codeTypeGrid").datagrid('getPager').pagination({pageNumber:1});
}

function showEdit(codeTypeId){
	var url = appPath+'/system/code_showEditById.do?codeTypeId=' + codeTypeId;
	$.post(url,function(data){
			 $("#codeTypeId").attr("value",data.codeTypeId);
			 $("#codeTypeValue").attr("value",data.codeTypeValue);
			 $("#codeTypeRemark").text(data.codeTypeRemark);
		}
	);
	
	$('#addDlg').window({
		iconCls:'icon-edit',
		width:600,
		height:300,
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#addDlg').window('move',{top:20});
	$('#addDlg').window('open');
	
}

function saveCodeType(){
	if($('#addForm').form('validate')){
		$.ajax({
			url:appPath+'/system/code_saveEditById.do',
			type:'post',
			data:{codeTypeId:$("#codeTypeId").val(),codeTypeValue:$("#codeTypeValue").val(),codeTypeRemark:$("#codeTypeRemark").val()},
			success:function(data){
				closeAddDlg();
				$("#codeTypeGrid").datagrid('reload');
				
			}
			
		}
	);
	}
}
function closeAddDlg(){
	$('#addDlg').window('close');
}

function showCode(codeTypeId){
	$("#codeTypeId").attr("value",codeTypeId);
	showCodeList(codeTypeId);
}

function showCodeList(codeTypeId){
	$("#codeGrid").datagrid({
		url:appPath+'/system/code_pagedCodeList.do?codeTypeId='+codeTypeId,
		nowrap: false,
		striped: true,
		remoteSort:false,
		idField:'codeId',
		resizable:true,
		singleSelect:true,
		columns:[[
				  {field:'codeNo',title:'操作',width:32,align:'center',
					  formatter:function(value,rowData,rowIndex){
						  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/cancel.png' title='删除' onClick='delCode(\""+rowData.codeId+"\",\""+codeTypeId+"\")'/></a>";
					  }
				  },
		          {field:'codeOrder',title:'序号',width:100,align:'center',sortable:false},
		          {field:'codeId',title:'代码编码',width:160,align:'center',sortable:false},
		          {field:'codeValue',title:'代码名称',width:160,align:'center',sortable:false,editor:{type:'validatebox',options:{required:true,validType:"length[0,40]"}}},
		          {field:'codeRemark',title:'备注',width:200,align:'center',sortable:false,editor:{type:'validatebox',options:{required:false,validType:"length[0,200]"}}}
		          
		        ]],
        pagination:false,
		rownumbers:false,
		toolbar:[{
			id:'btnadd',
			text:'新增',
			iconCls:'icon-add',
			handler:function(){
				addCode();
			}
		},{
			id:'btnsave',
			text:'保存',
			iconCls:'icon-save',
			handler:function(){
				doSaveCode();
			}
		}
		
		],
		onLoadSuccess:function(data){
//			alert("end");
			beginAllEdit();
		}
	}
	);
	
}

function delCode(codeId,codeTypeId){
	var url = appPath+"/system/code_deleteCodeById.do?codeId="+codeId+"&codeTypeId="+codeTypeId;
	$.ajax({
		url:url,
		type:'post',
		success:function(data){
			var row = $('#codeGrid').datagrid('getSelected');
			var index = $('#codeGrid').datagrid("getRowIndex",row);
			$('#codeGrid').datagrid('deleteRow', index);
			beginAllEdit();
		}
	});
	
}

function addCode(){
	var param = new Object();
	var row = new Object();
	row.codeOrder = '';
	row.codeId = '';
	row.codeValue = '';
	row.codeRemark = '';
	row.codeTypeId = $("#codeTypeId").val();
	param.row = row;
	changeCodeOrdeEdit();
	changeCodeIdEdit();
	$('#codeGrid').datagrid('insertRow', param);
	$('#codeGrid').datagrid('beginEdit', $('#codeGrid').datagrid('getRows').length-1);
	
}

function endAllEdit(){
	var rows = $('#codeGrid').datagrid('getRows');
	for(var j = 0; j < rows.length; j++){
		$('#codeGrid').datagrid('endEdit',j);
//		alert(j+"行uneditok");
	}
}
function beginAllEdit(){
	var rows = $('#codeGrid').datagrid('getRows');
	for(var j = 0; j <rows.length; j++){
		
		$('#codeGrid').datagrid('beginEdit',j);
//		$("#codeGrid").datagrid('removeEditor', 'codeOrder');
//		$("#codeGrid").datagrid('removeEditor', 'codeId');
//		alert(j+"行editok");
	}
}

function doSaveCode(){
	endAllEdit();
	var rows_u = $('#codeGrid').datagrid('getRows');
	var codes = new Array();
	var url = appPath+'/system/code_saveCode.do';
	
	var nos = new Array();
	for(var i = 0; i < rows_u.length; i++){
		var row = rows_u[i];
		if(!$("#codeGrid").datagrid("validateRow",i)){
			beginAllEdit();
			return;
		}
		for(var j = 0 ; j < nos.length; j++){
			if($.trim(nos[j]) == $.trim(row.codeId)){
				$.messager.alert('提示信息','代码编码不能有重复值','info');
				beginAllEdit();
				return;
			}
		}
		nos.unshift(row.codeId);		
		var arry = new Array();
		arry.unshift("\"codeId\":\""+row.codeId+"\"");
		arry.unshift("\"codeTypeId\":\""+row.codeTypeId+"\"");
		if(row.codeOrder == "")
			arry.unshift("\"codeOrder\":null");
		else 
			arry.unshift("\"codeOrder\":"+row.codeOrder);
		arry.unshift("\"codeValue\":\""+$.trim(row.codeValue)+"\"");
		arry.unshift("\"codeRemark\":\""+$.trim(row.codeRemark)+"\"");
		codes.unshift("{"+arry.toString()+"}");
	}
	$('#codeGrid').datagrid('acceptChanges');
	$.ajax({
		url:url,
		type:'post',
		data:{jasonContent:"["+codes.toString()+"]"},
		success:function(data){
			$.messager.alert('提示信息','保存成功','info');
			$("#codeGrid").datagrid('removeEditor', 'codeOrder');
			$("#codeGrid").datagrid('removeEditor', 'codeId');
			$("#codeGrid").datagrid('reload');
			//beginAllEdit();
		}
	   }
	);	
}

//定义为字段添加货删除可编辑属性的方法
$.extend($.fn.datagrid.methods, {
    addEditor : function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item.field);
                e.editor = item.editor;
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param.field);
            e.editor = param.editor;
        }
    },
    removeEditor : function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item);
                e.editor = {};
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param);
            e.editor = {};
        }
    }
});



function changeCodeOrdeEdit(){
	$("#codeGrid").datagrid('addEditor', {
	    field : 'codeOrder',
	    editor : {
	        type : 'numberbox',
	        options : {
	            required : {required:true,validType:"length[0,11]"}
	        }
	    }
	});
}

function changeCodeIdEdit(){
	$("#codeGrid").datagrid('addEditor', {
	    field : 'codeId',
	    editor : {
	        type : 'validatebox',
	        options : {
	            required : {required:true,validType:"length[0,4]"}
	        }
	    }
	});
}

function changeCodeEdit() {
	
	$("#codeGrid").datagrid('addEditor', {
	    field : 'codeValue',
	    editor : {
	        type : 'validatebox',
	        options : {
	            required : {required:true,validType:"length[0,40]"}
	        }
	    }
	});
	
	$("#codeGrid").datagrid('addEditor', {
	    field : 'codeRemark',
	    editor : {
	        type : 'validatebox',
	        options : {
	            required : {required:true,validType:"length[0,200]"}
	        }
	    }
	});
}


